package com.rxyb.hongyu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxyb.core.respone.IBaseCode;
import com.rxyb.core.respone.R;
import com.rxyb.core.respone.enums.CommonCode;
import com.rxyb.hongyu.constants.HongYuConstants;
import com.rxyb.hongyu.db.dao.ThirdHongyuPullMapper;
import com.rxyb.hongyu.db.po.ThirdHongyuCfg;
import com.rxyb.hongyu.db.po.ThirdHongyuPull;
import com.rxyb.hongyu.gateway.dto.HongYuRsp;
import com.rxyb.hongyu.gateway.dto.push.*;
import com.rxyb.hongyu.gateway.enums.RspCode;
import com.rxyb.hongyu.service.ThirdHongyuCfgService;
import com.rxyb.hongyu.service.ThirdHongyuPushService;
import com.rxyb.hongyu.service.logic.HongyuLocalLogic;
import com.rxyb.hongyu.service.logic.ThirdHongyuPushLogic;
import com.rxyb.hongyu.utils.HongYuClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p
 * 鸿宇请求的记录 服务实现类
 * </p
 *
 * @author DUYH
 * @since 2020-06-09
 */
@Service
public class ThirdHongyuPushServiceImpl extends ServiceImpl<ThirdHongyuPullMapper, ThirdHongyuPull> implements ThirdHongyuPushService {

    @Autowired
    private HongYuClient hongYuClient;
    @Autowired
    private HongyuLocalLogic hongyuLocalLogic;
    @Autowired
    private ThirdHongyuPushLogic thirdHongyuPushLogic;
    @Autowired
    private ThirdHongyuCfgService thirdHongyuCfgService;

    @Override
    public R<F20A01Rsp> f20A01(F20A01Req f20A01Req) {
        ThirdHongyuCfg cfg = thirdHongyuCfgService.queryByJyjgbh(f20A01Req.getJyjgbh());
        if (null == cfg) {
            return R.failed(RspCode.MCHNT_NOT_EXSIT);
        }
        HongYuRsp hongYuRsp;
        if (HongYuConstants.XINCHE.equals(f20A01Req.getSyr())) {
            // 新车系统自行处理
            hongYuRsp = hongyuLocalLogic.registerCar(f20A01Req);
            cfg.setPostUrl("挡板");
        } else if (HongYuConstants.isWall) {
            // 挡板
            hongYuRsp = hongyuLocalLogic.f20A01(f20A01Req);
            cfg.setPostUrl("挡板");
        } else {
            hongYuRsp = hongYuClient.post(cfg, HongYuConstants.FUNC_20A01, JSONObject.toJSONString(f20A01Req));
            // 特殊处理5-10秒的问题
            if (!HongYuConstants.isSucc(hongYuRsp.getRetcode())) {
                // 防止该描述对应的code不是等待处理
                if (hongYuRsp.getRetmessage().indexOf("查询请求已发送，请确认号牌信息无误后，等待5-10秒再试！") == 0) {
                    hongYuRsp.setRetcode(HongYuConstants.WAIT);
                }
            }
        }
        // 保存记录
        thirdHongyuPushLogic.saveHongyu(HongYuConstants.FUNC_20A01, cfg, f20A01Req, hongYuRsp);
        // 封装返回报文
        RspCode rspcode = hongyuLocalLogic.parse(hongYuRsp);
        if (null == rspcode) {
            F20A01Rsp f20A01Rsp = JSONObject.parseObject(hongYuRsp.getVehqueryinfo(), F20A01Rsp.class);
            return R.ok(f20A01Rsp);
        }
        return R.failed(rspcode);
    }

    @Override
    public R<F20A02Rsp> f20A02(F20A02Req f20A02Req) {
        ThirdHongyuCfg cfg = thirdHongyuCfgService.queryByJyjgbh(f20A02Req.getJyjgbh());
        if (null == cfg) {
            return R.failed(RspCode.MCHNT_NOT_EXSIT);
        }
        HongYuRsp hongYuRsp = null;
        // 挡板
        if (HongYuConstants.isWall) {
            hongYuRsp = hongyuLocalLogic.f20A02(f20A02Req);
            cfg.setPostUrl("挡板");
        } else {
            hongYuRsp = hongYuClient.post(cfg, HongYuConstants.FUNC_20A02, JSONObject.toJSONString(f20A02Req));
        }
        // 保存记录
        thirdHongyuPushLogic.saveHongyu(HongYuConstants.FUNC_20A02, cfg, f20A02Req, hongYuRsp);
        // 封装返回报文
        RspCode rspcode = hongyuLocalLogic.parse(hongYuRsp);
        if (null == rspcode) {
            F20A02Rsp f20A02Rsp = JSONObject.parseObject(hongYuRsp.getVehqueryinfo(), F20A02Rsp.class);
            return R.ok(f20A02Rsp);
        }
        return R.failed(rspcode);
    }

    @Override
    public R<F20A03Rsp> f20A03(F20A03Req f20A03Req) {
        ThirdHongyuCfg cfg = thirdHongyuCfgService.queryByJyjgbh(f20A03Req.getJyjgbh());
        if (null == cfg) {
            return R.failed(RspCode.MCHNT_NOT_EXSIT);
        }
        HongYuRsp hongYuRsp;
        // 挡板
        if (HongYuConstants.isWall) {
            hongYuRsp = hongyuLocalLogic.f20A03(f20A03Req);
            cfg.setPostUrl("挡板");
        } else {
            hongYuRsp = hongYuClient.post(cfg, HongYuConstants.FUNC_20A03, JSONObject.toJSONString(f20A03Req));
        }
        // 保存记录
        thirdHongyuPushLogic.saveHongyu(HongYuConstants.FUNC_20A03, cfg, f20A03Req, hongYuRsp);
        // 封装返回报文
        RspCode rspcode = hongyuLocalLogic.parse(hongYuRsp);
        if (null == rspcode) {
            F20A03Rsp f20A03Rsp = JSONObject.parseObject(hongYuRsp.getVehqueryinfo(), F20A03Rsp.class);
            return R.ok(f20A03Rsp);
        }
        return R.failed(rspcode);
    }

    @Override
    public R<IBaseCode> f20A04(F20A04Req f20A04Req) {
        ThirdHongyuCfg cfg = thirdHongyuCfgService.queryByJyjgbh(f20A04Req.getJyjgbh());
        if (null == cfg) {
            return R.failed(RspCode.MCHNT_NOT_EXSIT);
        }
        HongYuRsp hongYuRsp;
        // 挡板
        if (HongYuConstants.isWall) {
            hongYuRsp = hongyuLocalLogic.f20A04(f20A04Req);
            cfg.setPostUrl("挡板");
        } else {
            hongYuRsp = hongYuClient.post(cfg, HongYuConstants.FUNC_20A04, JSONObject.toJSONString(f20A04Req));
        }
        // 保存记录
        thirdHongyuPushLogic.saveHongyu(HongYuConstants.FUNC_20A04, cfg, f20A04Req, hongYuRsp);
        // 封装返回报文
        RspCode rspcode = hongyuLocalLogic.parse(hongYuRsp);
        if (null == rspcode) {
            return R.ok(CommonCode.SUCCESS);
        }
        return R.failed(rspcode);
    }

    @Override
    public R<IBaseCode> f20A05(F20A05Req f20A05Req) {
        HongYuRsp hongYuRsp;
        ThirdHongyuCfg cfg = thirdHongyuCfgService.queryByJyjgbh(f20A05Req.getJyjgbh());
        if (null == cfg) {
            return R.failed(RspCode.MCHNT_NOT_EXSIT);
        }
        // 挡板
        if (HongYuConstants.isWall) {
            hongYuRsp = hongyuLocalLogic.f20A05(f20A05Req);
            cfg.setPostUrl("挡板");
        } else {
            hongYuRsp = hongYuClient.post(cfg, HongYuConstants.FUNC_20A05, JSONObject.toJSONString(f20A05Req));
        }
        // 保存记录
        thirdHongyuPushLogic.saveHongyu(HongYuConstants.FUNC_20A05, cfg, f20A05Req, hongYuRsp);
        // 封装返回报文
        RspCode rspcode = hongyuLocalLogic.parse(hongYuRsp);
        if (null == rspcode) {
            return R.ok(CommonCode.SUCCESS);
        }
        return R.failed(rspcode);
    }

    @Override
    public R<IBaseCode> f20A06(F20A06Req f20A06Req) {
        HongYuRsp hongYuRsp;
        ThirdHongyuCfg cfg = thirdHongyuCfgService.queryByJyjgbh(f20A06Req.getJyjgbh());
        if (null == cfg) {
            return R.failed(RspCode.MCHNT_NOT_EXSIT);
        }
        // 挡板
        if (HongYuConstants.isWall) {
            hongYuRsp = hongyuLocalLogic.f20A06(f20A06Req);
            cfg.setPostUrl("挡板");
        } else {
            hongYuRsp = hongYuClient.post(cfg, HongYuConstants.FUNC_20A06, JSONObject.toJSONString(f20A06Req));
        }
        // 保存记录
        thirdHongyuPushLogic.saveHongyu(HongYuConstants.FUNC_20A06, cfg, f20A06Req, hongYuRsp);
        // 封装返回报文
        RspCode rspcode = hongyuLocalLogic.parse(hongYuRsp);
        if (null == rspcode) {
            return R.ok(CommonCode.SUCCESS);
        }
        return R.failed(rspcode);
    }
}
